<?php

namespace app\api\model\project;

use app\api\model\project\ProjectEvaluate as ProjectEvaluateModel;
use think\Db;
use think\Model;


class ProjectEvaluate extends Model
{





    // 表名
    protected $name = 'project_evaluate';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];




    public function add($params)
    {
        Db::startTrans();

        try {
            $video = $params['video'];
            $content = $params['content'];
            $images = $params['images'];
            $order_id = $params['order_id'];
            $staff_id = $params['staff_id'];
            $apply_id = $params['apply_id'];
            $desccredit = $params['desccredit'];
            $servicecredit = $params['servicecredit'];
            $user_id = $params['user_id'];
            if (!$order_id&&!$staff_id)throw new \Exception('参数缺少');
            if (empty($content))throw new \Exception('请输入评价内容');
            if (!$desccredit)throw new \Exception('请选择描述分');
            if (!$servicecredit)throw new \Exception('请选择服务分');
            $info = ProjectEvaluateModel::get(['user_id'=>$user_id, 'order_id'=>$order_id]);
            if ($info)throw new \Exception('已评价');
            $evaluate_model = new ProjectEvaluateModel;

            $data = [
                'user_id'=>$user_id,
                'order_id'=>$order_id,
                'staff_id'=>$staff_id,
                'apply_id'=>$apply_id,
                'content'=>$content,
                'images'=>$images,
                'video'=>$video,
                'desccredit'=>$desccredit,
                'servicecredit'=>$servicecredit
            ];

            $res = $evaluate_model->save($data);
            if ($res){
                ProjectStaff::where(['id'=>$staff_id])->setInc('comment_num');
                \app\api\model\project\ProjectOrder::update(['is_evaluate'=>1],['order_id'=>$order_id]);
                $fen_data = [
                    'desccredit'=>$data['desccredit'],
                    'servicecredit'=>$data['servicecredit']
                ];
                $this->staffEvaluate($order_id,$fen_data);
                Db::commit();
                return true;
            } else {
                Db::rollback();
                throw new \Exception('评价失败');
            }


        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new \Exception($e->getMessage().'-'.$e->getLine());
        }

    }



    /**
     * 评价后 计算人员评价
     * @param $site_id
     * @param $evaluate
     * @param $num
     */
    public function staffEvaluate($order_id, $evaluate)
    {

        $order_info = ProjectOrder::where(['order_id'=>$order_id])->field('staff_id')->find();

        $staff_id = $order_info["staff_id"];
        //$num = model("order_goods")->getCount([["order_id", "=", $order_id]], "order_goods_id");
        $num = ProjectEvaluate::where(['order_id'=>$order_id])->count();
        $count = ProjectEvaluate::where(['order_id'=>$order_id])->count();

        $shop_info = ProjectStaff::where(['id'=>$staff_id])->field('id,desccredit,servicecredit')->find();
        $last_count = $count + $num;
        //控制非法分值

        foreach ($evaluate as $k => $v) {
            if ($v < 60) {
                $v = 60;
            }
            if ($v > 100) {
                $v = 100;
            }
            $evaluate[$k] = $v;
        }
        $data = [];
        if ($evaluate["desccredit"] > 0) {

            $avg_desccredit = ($shop_info["desccredit"] * $count + $evaluate["desccredit"] * $num) / $last_count;
            $data["desccredit"] = $avg_desccredit;
        }
        if ($evaluate["servicecredit"] > 0) {
            $avg_servicecredit = ($shop_info["servicecredit"] * $count + $evaluate["servicecredit"] * $num) / $last_count;
            $data["servicecredit"] = $avg_servicecredit;
        }

        if (!empty($data)) {
            $result = ProjectStaff::update($data,['id'=>$staff_id]);

        } else {
            $result = 1;
        }

        return $result;
    }

}
